package com.ptj.controller;

import java.util.Map;

import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ptj.entity.User;
import com.ptj.service.UserServiceImpl;

@Controller
public class UserController {
	@Resource
	private UserServiceImpl usi;
	/*
	 * 登录验证
	 */
	@RequestMapping("/login")
	public String userLogin(User user, @Param("userphone")String userphone,@Param("password")String password,
			Model model,HttpSession session,RedirectAttributes attr){
		Pattern pattern = Pattern.compile("/^(\\w){6,20}$/");
		if (!(pattern.matcher(user.getPassword()).matches())) {
			model.addAttribute("password","密码必须为6-18位的数字和字母组合!");
		}
		pattern = Pattern.compile("0?(13|14|15|18|17)[0-9]{9}");
		if (!(pattern.matcher(user.getUserphone()).matches())) {
			model.addAttribute("phone","请输入正确的手机号！");
		}
		User user1=usi.userLogin(userphone,password);
		System.out.println(user);
		if(user1!=null){
				session.setAttribute("user", user1);
				System.out.println(user1.getId());
				model.addAttribute("user",user1);
				System.out.println("登录成功");
				return "/WEB-INF/Index.jsp";
			}else{
				model.addAttribute("msg","登录失败！");
				model.addAttribute("userphone",user.getUserphone());
				System.out.println("登录失败");
				return "/Login.jsp";
			}
		
	}
	/*
	 * 注册验证
	 */
	@RequestMapping("/register")
	public String register(@Valid User user,BindingResult re,@Param("password1")String password1,Model model){
		//调用Validator工具类获取结果
				Map<String,Object>map=UserServiceImpl.getValidator(re);
				User a=usi.selectUserByPhone(user.getUserphone());
				User b=usi.selectUserByName(user.getUsername());
				if(b!=null){
					model.addAttribute("name","此用户名已存在！");
				}
					if(a!=null){
					model.addAttribute("phone","此手机号已被注册！");
					}
						if(!(user.getPassword().equals(password1))){
							model.addAttribute("password1","确认密码错误！");
						}
						Pattern pattern = Pattern.compile("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/");
						if (!(pattern.matcher(user.getPassword()).matches())) {
							model.addAttribute("password","密码必须为6-18位的数字和字母组合！");
						}
						if(map==null){
							//无错误
							usi.register(user);
							model.addAttribute("msg","注册成功！");
							return "/Login.jsp";
						}else{
							//有错，获取错误信息,实现跳转
							model.addAttribute("error",map);
							//把student对象存进jsp对象中
							model.addAttribute("user",user);
							System.out.println(map);
							return "/Register.jsp";
						}	
	}
	
    /*
     * 传id去用户信息完善
     */
	@RequestMapping("/goaddUser")
	public String goaddUser(User user,Model model){
			model.addAttribute("id",user.getId());
			return "/addUser.jsp";
	}
	
	/*
	 * 用户信息完善
	 */
	@RequestMapping("/addUser")
	public String addUser(User user,Model model){
		if (user.getUserimg() == null || user.getUserimg().equals("")) {
			model.addAttribute("userimg","请添加头像！");
		}
		Pattern pattern = Pattern.compile("/^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$/");
		if (!(pattern.matcher(user.getPassword()).matches())) {
			model.addAttribute("idcard","身份证格式错误！");
			return "/updateUser.jsp";
		}else{
			usi.addUser(user);
			model.addAttribute("mag","信息已完善！");
			model.addAttribute("user",user);
			return "/WEB-INF/Index.jsp";
		}
			
	}
	
	/*
	 * to用户信息修改
	 */
	@RequestMapping("/toupdateUser")
	public String toupdateUser(@Param("id") Integer id,Model model){
			User user=usi.selectUserByid(id);
			System.out.println(user);
			model.addAttribute("user",user);
			return "/updateUser.jsp";
	}
	
	/*
	 * 用户信息修改
	 */
	@RequestMapping("/updateUser")
	public String updateUser(@Valid User user,BindingResult re,@Param("password1")String password1, Model model){
		//调用Validator工具类获取结果
		System.out.println(user);
		Map<String,Object>map=UserServiceImpl.getValidator(re);
				if(!(user.getPassword().equals(password1))){
					model.addAttribute("password1","旧密码错误！");
			}
				Pattern pattern = Pattern.compile("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$/");
				if (!(pattern.matcher(user.getPassword()).matches())) {
					model.addAttribute("password","密码必须为6-18位的数字和字母组合！");
				}
				pattern = Pattern.compile("/^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$/");
				if (!(pattern.matcher(user.getIdcard()).matches())) {
					model.addAttribute("idcard","身份证格式错误！");
				}
				if(map==null){
					//无错误
					System.out.println(user);
					usi.updateUser(user);
					model.addAttribute("msg","修改成功！");
					model.addAttribute("id",user.getId());
					return "selectIndex";
				}else{
					//有错，获取错误信息,实现跳转
					model.addAttribute("error",map);
					//把student对象存进jsp对象中
					model.addAttribute("user",user);
					System.out.println(map);
					return "/updateUser.jsp";
				}
	}
	
	/*
	 * 用户删除
	 */
	@RequestMapping("/deleteUser")
	public String deleteUser(int id,Model model){
		usi.deleteUser(id);
		model.addAttribute("msg","账户删除成功！");
		return "Register.jsp";
	}
	
	/*
	 * 用户退出登录
	 */
	@RequestMapping("/userQuit")
	public String userQuit(HttpSession session){
		//清除session
		session.invalidate();
		return "Login.jsp";
	}
	/*
	 * 回首页
	 */
	@RequestMapping("/selectIndex")
	public String selectIndex(@Param("id")Integer id,Model model){
			System.out.println(id);
			User user=usi.selectUserByid(id);
			System.out.println(user);
			model.addAttribute("user",user);
			return "/WEB-INF/Index.jsp";
			
	}
	
}
